package com.afd.ozblip.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.afd.ozblip.adapter.DishListAdapter;
import com.afd.ozblip.adapter.PhotoListAdapter;
import com.afd.ozblip.entity.BusinessPhotoEntity;
import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.ParseJson;
import com.afd.ozblip.util.StringUtil;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

public class BusinessPhotoActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private GridView photoGV;
	private TextView topTitleTv;
	private RadioGroup photoRg;

	/****************** 数据变量 **************************/
	private List<BusinessPhotoEntity> photoList;
	private PhotoListAdapter photoAdapter;
	private String businessId;
	private String businessName;
	private String parentClassName;
	private String childClassName;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.business_other_photo);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
		// 加载数据
		requestPhotoListData();
	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.photoRg = (RadioGroup) findViewById(R.id.business_photo_group_rg);
		this.photoGV = (GridView) findViewById(R.id.gv_business_photo_list);
		this.topTitleTv = (TextView) findViewById(R.id.tv_business_photo_topTitle);
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {
		View.OnClickListener moreClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.ll_more_layout_setting:
					Intent settingIntent = new Intent(
							BusinessPhotoActivity.this, SettingActivity.class);
					startActivity(settingIntent);
					break;

				default:
					break;
				}
			}
		};
		this.photoGV
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int position, long arg3) {
						Intent imageIntent = new Intent(
								BusinessPhotoActivity.this,
								ImageShowActivity.class);
						BusinessPhotoEntity bpe = photoAdapter.dataList
								.get(position);
						imageIntent.putExtra("imageUrl", bpe.getImgMaxUrl());
						startActivity(imageIntent);
					}

				});
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		Intent intent = getIntent();
		this.businessId = intent.getStringExtra("businessId");
		this.businessName = intent.getStringExtra("businessName");
		this.parentClassName = intent.getStringExtra("parentClassName");
		this.childClassName = intent.getStringExtra("childClassName");
		this.topTitleTv.setText(businessName);
		if ("美食".equals(parentClassName) || "快餐".equals(childClassName)) {
			this.photoRg.setVisibility(0);
		}
	}

	/**
	 * 获取评论列表数据
	 */
	private void requestPhotoListData() {
		if (StringUtil.isNullOrEmpty(businessId)) {
			return;
		}
		AsyncLoadData ald = new AsyncLoadData(this, "加载中...",
				new AsyncAction() {

					@Override
					public void doLoad() throws Exception {
						// 获取提交参数
						Map<String, Object> nearbyBusinessParamMap = new HashMap<String, Object>();
						nearbyBusinessParamMap.put("sellerId", businessId);
						nearbyBusinessParamMap.put("imgType", "0");
						nearbyBusinessParamMap.put("pageCnt", "1");
						nearbyBusinessParamMap.put("pageRow", "20");
						nearbyBusinessParamMap.put("sort", "1");
						nearbyBusinessParamMap.put("language", commonLanguage);
						// 发送请求
						byte[] commentItemList = HttpUtil.requestByPost(
								BusinessPhotoActivity.this,
								Constants.SELLER_PHOTO_LIST_API,
								nearbyBusinessParamMap);
						if (commentItemList != null) {
							photoList = new ParseJson()
									.parseBusinessPhotoListJson(commentItemList);
						}
					}

					@Override
					public void loadEnd() throws Exception {
						setDishListAdapter();
					}

				});
		ald.execute(0);
	}

	/**
	 * 调用菜单列表适配器
	 */
	public void setDishListAdapter() {
		if (CollectionUtil.UnNullAndZero(photoList)) {
			this.photoAdapter = new PhotoListAdapter(BusinessPhotoActivity.this);
			this.photoAdapter.addItems(photoList);
			this.photoGV.setAdapter(this.photoAdapter);
		}
	}
}